﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PageEntity;
using IBLL;
using BLLImp;

namespace OASystem.Controllers
{
    public class ModuleController : BaseController
    {
        IModuleServer moduleserver;
        public ModuleController()
        {
            moduleserver = new ModuleServer();

        }
        public ActionResult Index()
        {
            SetTheme();
            return View();
        }
        public ActionResult ListView(long? id)
        {
            DataGridDTO model = moduleserver.LoadMoudleGridTree(id);
            return Json(model);

        }
        [HttpGet]
        public ActionResult Add()
        {
            return PartialView();
        }
        [HttpPost]
        public ActionResult Add(ModuleDTO mode)
        {
            MyJsonResult result = new MyJsonResult();
            try
            {

                moduleserver.Save(mode);
                result.Result = true;
                result.Obj = mode;
                return Json(result, "text/html", JsonRequestBehavior.AllowGet);

            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e )
            {

                result.Result = false;
                return Json(result, "text/html", JsonRequestBehavior.AllowGet); ;
            }

        }
        public ActionResult Delet(long ModuleID)
        {
            if (this.Request.IsAjaxRequest())
            {
                if (ModuleID <= 0) return Json(new MyJsonResult() { Result = false });
                var result = moduleserver.Delet(new ModuleDTO() { ModuleID = ModuleID });
                return Json(new MyJsonResult() { Result = result });

            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        public ActionResult LoadTreeNodes(int id = 0)
        {
            var nodes = moduleserver.LoadModuleTree(id);
            return Json(nodes);
        }
    }
}
